﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using KenLib.BusinessManager;
using System.Collections.Generic;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using DataInfo;
using System.Text;
using System.Web.Services;

public partial class JOINT_frmJ_TraineeSearch : System.Web.UI.Page
{
    本社情報Manager _本社情報Manager = new 本社情報Manager();
    事業内容Manager _事業内容Manager = new 事業内容Manager();
    応募情報Manager _応募情報Manager = new 応募情報Manager();

    /// <summary>
    /// Coder: Thanh Dong
    /// Event: Page load
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        ((JOINT_MstPage_JOINT)this.Master).HideLiArea("frmJ_TraineeSearch");
        ((JOINT_MstPage_JOINT)this.Master).changeTitle("候補者検索");

        if (Session["人コード"] == null)
        {
            Response.Redirect("../frmD_Login.aspx");
        }
        RFV_ddl_組合選択.ErrorMessage = GetError("ER00007");
        RFV_ddl_組合員企業選択.ErrorMessage = GetError("ER00007");
        RFV_面接日.ErrorMessage = GetError("ER00001");
        CV_面接日.ErrorMessage = GetError("ER00005");
        CV_面接日1.ErrorMessage = GetError("ER00005");

        if (!IsPostBack)
        {
            Session.Remove("frmJ_TraineeSearch_flag");
            txt_面接日.Focus();
            Load_ddl_組合選択();
            Load_ddl_組合員企業選択(-1);
            Load_ddl_年齢From();
            Load_ddl_年齢To();
        }
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Event: Set session for from frmJ_TraineeListSelect.aspx
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn候補者選択_Click(object sender, EventArgs e)
    {
        if (Convert.ToInt32(Session["frmJ_TraineeSearch_flag"]) != 1)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('" + GetError("ER00008") + "');</script>");
            return;
        }
        DataTable dt = frmJ_TraineeSearch_Search();
        if (dt.Rows.Count == 0)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('" + GetError("ER00008") + "');</script>");
            return;
        }
        else
        {
            DataTable dt1 = frmJ_TraineeSearch_CheckExists(Convert.ToDateTime(txt_面接日.Text), Convert.ToInt32(ddl_組合選択.SelectedValue), Convert.ToInt32(ddl_組合員企業選択.SelectedValue));
            if (dt1.Rows.Count > 0)
            {
                this.mpe_Confirm.Show();
            }
            else
            {
                Session["frmJ_TraineeSearch_Mode"] = "insert";
                Session["frmJ_TraineeSearch_Search"] = frmJ_TraineeSearch_Search();
                Session["frmJ_TraineeSearch_ddl_組合選択"] = Convert.ToInt32(ddl_組合選択.SelectedValue);
                Session["frmJ_TraineeSearch_ddl_組合選択_text"] = ddl_組合選択.SelectedItem.Text;
                Session["frmJ_TraineeSearch_ddl_組合員企業選択"] = Convert.ToInt32(ddl_組合員企業選択.SelectedValue);
                Session["frmJ_TraineeSearch_ddl_組合員企業選択_Text"] = ddl_組合員企業選択.SelectedItem.Text;
                Session["frmJ_TraineeSearch_txt_面接日"] = txt_面接日.Text;
                Response.Redirect("frmJ_TraineeListSelect.aspx");
            }
            
        }
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Load data in ddl_組合選択
    /// </summary>
    public void Load_ddl_組合選択()
    {
        DataTable dt = new DataTable();
        dt = _本社情報Manager.frmJ_TraineeSearch_Select_dll組合選択();
        ddl_組合選択.DataSource = dt;
        ddl_組合選択.DataTextField = "企業名";
        ddl_組合選択.DataValueField = "企業ID";
        ddl_組合選択.DataBind();
        System.Web.UI.WebControls.ListItem item = new System.Web.UI.WebControls.ListItem("[選択]", "-1");
        item.Selected = true;
        ddl_組合選択.Items.Insert(0, item);
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Load data in ddl_組合員企業選択
    /// </summary>
    /// 
    public void Load_ddl_組合員企業選択(int 企業ID)
    {
        DataTable dt = new DataTable();
        dt = _本社情報Manager.frmJ_TraineeSearch_Select_dll組合員企業選択(企業ID);
        ddl_組合員企業選択.DataSource = dt;
        ddl_組合員企業選択.DataTextField = "企業名";
        ddl_組合員企業選択.DataValueField = "企業ID";
        ddl_組合員企業選択.DataBind();
        System.Web.UI.WebControls.ListItem item = new System.Web.UI.WebControls.ListItem("[選択]", "-1");
        item.Selected = true;
        ddl_組合員企業選択.Items.Insert(0, item);
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Load data in ddl_年齢From 18 -> 35
    /// </summary>
    public void Load_ddl_年齢From()
    {
        List<string> list = new List<string>();
        //list.Add("----選択----");
        for (int i = 18; i <= 35; i++)
        {
            list.Add(i.ToString());
        }
        ddl_年齢1.DataSource = list;
        ddl_年齢1.DataBind();
        System.Web.UI.WebControls.ListItem item = new System.Web.UI.WebControls.ListItem("[選択]", "-1");
        item.Selected = true;
        ddl_年齢1.Items.Insert(0, item);
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Load data in ddl_年齢To 18 -> 35
    /// </summary>
    public void Load_ddl_年齢To()
    {
        List<string> list = new List<string>();
        //list.Add("----選択----");
        for (int i = 18; i <= 35; i++)
        {
            list.Add(i.ToString());
        }
        ddl_年齢2.DataSource = list;
        ddl_年齢2.DataBind();
        System.Web.UI.WebControls.ListItem item = new System.Web.UI.WebControls.ListItem("[選択]", "-1");
        item.Selected = true;
        ddl_年齢2.Items.Insert(0, item);
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Load data ddl_経験作業名
    /// </summary>
    public void Load_ddl_経験作業名()
    {
        DataTable dt = new DataTable();
        dt = _事業内容Manager.frmJ_TraineeSearch_Select_ddl_経験作業名();
        ddl_経験作業名.DataSource = dt;
        ddl_経験作業名.DataTextField = "事業内容";
        ddl_経験作業名.DataValueField = "ID";
        ddl_経験作業名.DataBind();
        System.Web.UI.WebControls.ListItem item = new System.Web.UI.WebControls.ListItem("[選択]", "-1");
        item.Selected = true;
        ddl_経験作業名.Items.Insert(0, item);
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Search data
    /// </summary>
    /// <param name="年齢1"></param>
    /// <param name="年齢2"></param>
    /// <param name="性別"></param>
    /// <param name="最終学歴1"></param>
    /// <param name="最終学歴2"></param>
    /// <param name="就業経験有無"></param>
    /// <param name="身長"></param>
    /// <param name="体重1"></param>
    /// <param name="体重2"></param>
    /// <param name="視力"></param>
    /// <param name="日本語学習経験"></param>
    /// <param name="経験作業名"></param>
    /// <returns></returns>
    public DataTable frmJ_TraineeSearch_Search(int 年齢1, int 年齢2, bool? 性別, int 最終学歴1, int 最終学歴2, bool? 就業経験有無, int 身長, int 体重1, int 体重2, int 視力, int 日本語学習経験, int 経験作業名, DateTime? 面接日, int 団体ID, int 企業ID)
    {
        DataTable dt = new DataTable();
        dt = _応募情報Manager.frmJ_TraineeSearch_Search(年齢1, 年齢2, 性別, 最終学歴1, 最終学歴2, 就業経験有無, 身長, 体重1, 体重2, 視力, 日本語学習経験, 経験作業名, 面接日, 団体ID, 企業ID);
        return dt;
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Event: Search data
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn_検索_Click(object sender, EventArgs e)
    {
        DataTable dt = frmJ_TraineeSearch_Search();
        lbl_Count.Text = dt.Rows.Count.ToString() + " 人をヒットしました。";
        Session["frmJ_TraineeSearch_flag"] = 1;
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Event: Export file CSV
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnCSV出力_Click(object sender, EventArgs e)
    {
        if (Convert.ToInt32(Session["frmJ_TraineeSearch_flag"]) != 1)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('" + GetError("ER00008") + "');</script>");
            return;
        }
        DataTable dt = ConvertToDatable(frmJ_TraineeSearch_Search());
        if (dt.Rows.Count == 0)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('" + GetError("ER00008") + "');</script>");
            return;
        }
        else
        {
            ExportCSV(dt);
        }
    }


    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Export file csv
    /// </summary>
    /// <param name="dt"></param>
    public void ExportCSV(DataTable dt)
    {
        string stOutput = "";
        string sHeaders = "";
        for (int i = 0; i < dt.Columns.Count; i++)
        {
            sHeaders = sHeaders.ToString() + Convert.ToString(dt.Columns[i]) + ", ";
        }
        stOutput += sHeaders + "\r\n";
        // Export data.  

        foreach (DataRow dr in dt.Rows)
        {
            string stLine = "";
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                stLine = stLine.ToString() + Convert.ToString(dr[i]).Replace(",", " -") + ",";
            }
            stOutput += stLine + "\r\n";
        }
        stOutput = stOutput.Replace("/", " l ");
        //stOutput = stOutput.Replace("-", " ");
        //stOutput = stOutput.Replace("  ", " ");
        string path = Server.MapPath("../CSV/");
        //string path = "";
        //Response.AddHeader("content-disposition","attachment;filename=GridViewExport.csv");
        string filename = "TraineeList_" + string.Format("{0:yyyyMMdd}", DateTime.Now) + ".csv";
        System.IO.FileStream fs = new System.IO.FileStream(path + filename, System.IO.FileMode.Create);
        StreamWriter bw = new StreamWriter(fs, System.Text.Encoding.UTF8);
        bw.Write(stOutput);
        bw.Flush();
        bw.Close();
        fs.Close();
        ClientScript.RegisterStartupScript(this.GetType(), "dw", "window.open('" + Request.Url.AbsoluteUri.Replace("JOINT/frmJ_TraineeSearch.aspx", "CSV/") + filename + "')", true);
    }


    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Return data in datatable
    /// </summary>
    /// <returns></returns>
    public DataTable frmJ_TraineeSearch_Search()
    {
        int 年齢1;
        if (Convert.ToInt32(ddl_年齢1.SelectedValue) == -1)
        {
            年齢1 = 18;
        }
        else
        {
            年齢1 = Convert.ToInt32(ddl_年齢1.SelectedValue);
        }
        int 年齢2;
        if (Convert.ToInt32(ddl_年齢2.SelectedValue) == -1)
        {
            年齢2 = 35;
        }
        else
        {
            年齢2 = Convert.ToInt32(ddl_年齢2.SelectedValue);
        }

        bool? 性別;
        if (ddl_性別.SelectedValue.ToString() == "-1")
        {
            性別 = null;
        }
        else if (ddl_性別.SelectedValue.ToString() == "1")
        {
            性別 = true;
        }
        else
        {
            性別 = false;
        }
        int 最終学歴1;
        if (Convert.ToInt32(ddl_最終学歴1.SelectedValue) == -1)
        {
            最終学歴1 = 0;
        }
        else
        {
            最終学歴1 = Convert.ToInt32(ddl_最終学歴1.SelectedValue);
        }

        int 最終学歴2;
        if (Convert.ToInt32(ddl_最終学歴2.SelectedValue) == -1)
        {
            最終学歴2 = 5;
        }
        else
        {
            最終学歴2 = Convert.ToInt32(ddl_最終学歴2.SelectedValue);
        }
        bool? 就業経験有無;
        if (ddl_就業経験有無.SelectedValue.ToString() == "-1")
        {
            就業経験有無 = null;
        }
        else if (ddl_就業経験有無.SelectedValue.ToString() == "1")
        {
            就業経験有無 = true;
        }
        else
        {
            就業経験有無 = false;
        }
        int 身長;
        if (txt_身長.Text == "")
        {
            身長 = -1;
        }
        else
        {
            身長 = Convert.ToInt32(txt_身長.Text);
        }
        int 体重1;
        if (txt_体重1.Text == "")
        {
            体重1 = 1;
        }
        else
        {
            体重1 = Convert.ToInt32(txt_体重1.Text);
        }

        int 体重2;
        if (txt_体重2.Text == "")
        {
            体重2 = 999;
        }
        else
        {
            体重2 = Convert.ToInt32(txt_体重2.Text);
        }

        int 視力;
        if (Convert.ToInt32(ddl_視力.SelectedValue) == -1)
        {
            視力 = -1;
        }
        else
        {
            視力 = Convert.ToInt32(ddl_視力.SelectedValue);
        }

        int 日本語学習経験;
        if (Convert.ToInt32(ddl_日本語学習経験.SelectedValue) == -1)
        {
            日本語学習経験 = -1;
        }
        else
        {
            日本語学習経験 = Convert.ToInt32(ddl_日本語学習経験.SelectedValue);
        }

        int 経験作業名;
        if (Convert.ToInt32(ddl_経験作業名.SelectedValue) == -1)
        {
            経験作業名 = -1;
        }
        else
        {
            経験作業名 = Convert.ToInt32(ddl_経験作業名.SelectedValue);
        }
        try
        {
            DateTime.Parse(txt_面接日.Text);
        }
        catch
        {
            txt_面接日.Text = "";
        }
        DateTime? 面接日;
        if (txt_面接日.Text == "")
        {
            面接日 = null;
        }
        else
        {
            面接日 = Convert.ToDateTime(txt_面接日.Text);
        }
        DataTable dt = new DataTable();
        dt = frmJ_TraineeSearch_Search(年齢1, 年齢2, 性別, 最終学歴1, 最終学歴2, 就業経験有無, 身長, 体重1, 体重2, 視力, 日本語学習経験, 経験作業名, 面接日, Convert.ToInt32(ddl_組合選択.SelectedValue), Convert.ToInt32(ddl_組合員企業選択.SelectedValue));
        return dt;
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Event: Export file pdf
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnPDF出力_Click(object sender, EventArgs e)
    {
        if (Convert.ToInt32(Session["frmJ_TraineeSearch_flag"]) != 1)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('" + GetError("ER00008") + "');</script>");
            return;
        }
        DataTable dt = ConvertToDatable(frmJ_TraineeSearch_Search());
        if (dt.Rows.Count == 0)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('" + GetError("ER00008") + "');</script>");
            return;
        }
        else
        {
            ExportPdfTable(dt);
        }
    }


    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Convert datatable to datable
    /// </summary>
    /// <param name="dt1"></param>
    /// <returns></returns>
    public DataTable ConvertToDatable(DataTable dt1)
    {
        DataTable dt = new DataTable();
        if (dt1.Rows.Count > 0)
        {
            dt.Columns.Add("No", typeof(string));
            dt.Columns.Add("登録年月日", typeof(string));
            dt.Columns.Add("氏名", typeof(string));
            dt.Columns.Add("性別", typeof(string));
            dt.Columns.Add("年齢", typeof(string));
            dt.Columns.Add("身長", typeof(string));
            dt.Columns.Add("体重", typeof(string));
            dt.Columns.Add("視力", typeof(string));
            dt.Columns.Add("最終学歴", typeof(string));
            dt.Columns.Add("在学状態", typeof(string));
            dt.Columns.Add("就業経験有無", typeof(string));
            dt.Columns.Add("経験作業名1", typeof(string));
            dt.Columns.Add("経験作業名2", typeof(string));
            dt.Columns.Add("経験作業名3", typeof(string));
            dt.Columns.Add("具体的仕事内容", typeof(string));
            dt.Columns.Add("日本語学習経験", typeof(string));
            dt.Columns.Add("現住所", typeof(string));
            dt.Columns.Add("出身地", typeof(string));

            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                int temp = i + 1;
                string No = Convert.ToString(temp);
                string 登録年月日 = "";
                if (dt1.Rows[i]["登録年月日"].ToString() != "")
                {
                    登録年月日 = String.Format("{0:yyyy年MM月dd日}", Convert.ToDateTime(dt1.Rows[i]["登録年月日"]));
                }
                else
                {
                    登録年月日 = "";
                }
                string 氏名 = dt1.Rows[i]["姓"].ToString() + " " + dt1.Rows[i]["名"].ToString();
                string 性別 = Convert性別(dt1.Rows[i]["性別"].ToString());
                string 年齢 = Conver年齢(dt1.Rows[i]["生年月日"].ToString()).ToString();
                string 身長 = dt1.Rows[i]["身長"].ToString();
                string 体重 = dt1.Rows[i]["体重"].ToString();
                string 視力 = Convert視力(dt1.Rows[i]["視力"].ToString());
                string 最終学歴 = Convert_最終学歴(dt1.Rows[i]["最終学歴"].ToString());
                string 在学状態 = dt1.Rows[i]["在学学校名"].ToString();
                string 就業経験有無 = Convert就業経験有無(dt1.Rows[i]["就業経験有無"].ToString());
                string 経験作業名1 = dt1.Rows[i]["経験作業名1"].ToString();
                string 経験作業名2 = dt1.Rows[i]["経験作業名2"].ToString();
                string 経験作業名3 = dt1.Rows[i]["経験作業名3"].ToString();
                string 具体的仕事内容 = dt1.Rows[i]["具体的仕事内容"].ToString();
                string 日本語学習経験 = Convert_日本語学習経験(dt1.Rows[i]["日本語学習経験"].ToString());
                string 現住所 = dt1.Rows[i]["現住所"].ToString();
                string 出身地 = dt1.Rows[i]["出身地"].ToString();

                dt.Rows.Add(No, 登録年月日, 氏名, 性別, 年齢, 身長, 体重, 視力, 最終学歴, 在学状態, 就業経験有無, 経験作業名1, 経験作業名2, 経験作業名3, 具体的仕事内容, 日本語学習経験, 現住所, 出身地);

            }
        }
        return dt;
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Convert number to string
    /// </summary>
    /// <param name="temp"></param>
    /// <returns></returns>
    public string Convert_日本語学習経験(string temp)
    {
        string str_Result = "";
        switch (temp)
        {
            case "1":
                str_Result = "Dưới 3 tháng";
                break;
            case "2":
                str_Result = "Trên 3 tháng";
                break;
            case "3":
                str_Result = "Trên 6 tháng";
                break;
            case "0":
                str_Result = "Không có";
                break;
            default:
                str_Result = "";
                break;
        }
        return str_Result;
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Convert number to string
    /// </summary>
    /// <param name="temp"></param>
    /// <returns></returns>
    public string Convert_最終学歴(string temp)
    {

        string str_Result = "";
        switch (temp)
        {
            case "0":
                str_Result = "Tiến Sỹ";
                break;
            case "1":
                str_Result = "Thạc Sỹ";
                break;
            case "2":
                str_Result = "Cử Nhân";
                break;
            case "3":
                str_Result = "Cao Đẳng";
                break;
            case "4":
                str_Result = "Tốt nghiệp Phổ Thông";
                break;
            default:
                str_Result = "Tốt nghiệp Trung Học";
                break;
        }
        return str_Result;
    }


    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Convert number to string
    /// </summary>
    /// <param name="temp"></param>
    /// <returns></returns>
    public string Convert視力(string temp)
    {
        string str_Result = "";
        if (temp == "0")
        {
            str_Result = "Bình thường";
        }
        else if (temp == "1")
        {
            str_Result = "Đeo kính";
        }
        else if (temp == "2")
        {
            str_Result = "Đeo kính áp tròng";
        }
        else
        {
            str_Result = "";
        }
        return str_Result;
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Convert bit to string (Nam, Nữ)
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public string Convert性別(string str)
    {
        string strtemp;
        if (str == "True")
        {
            strtemp = "Nữ";
        }
        else
        {
            strtemp = "Nam";
        }
        return strtemp;
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Get old  from type datetime
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public string Conver年齢(string str)
    {
        string temp = "";
        if (str == "")
        {
            temp = "";
            return temp;
        }
        DateTime dtDate = DateTime.Parse(str);
        int tuoi = DateTime.Now.Year - dtDate.Year;
        if (DateTime.Now.Month < dtDate.Month)
        {
            tuoi--;
        }
        else if (DateTime.Now.Month == dtDate.Month && DateTime.Now.Day < dtDate.Day)
        {
            tuoi--;
        }
        return Convert.ToString(tuoi);
    }


    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Convert bit to string
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public string Convert就業経験有無(string str)
    {

        if (str == "")
        {
            str = "";
        }
        else if (Convert.ToBoolean(str) == false)
        {
            str = "Không";
        }
        else
        {
            str = "Có";
        }
        return str;
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Export to file pdf
    /// </summary>
    /// <param name="dt"></param>
    public void ExportPdfTable(DataTable dt)
    {
        int cols = dt.Columns.Count;
        int rows = dt.Rows.Count;
        Document doc = new Document(PageSize.A4, 2f, 4f, 2f, 4f); //iTextSharp document
        doc.SetPageSize(iTextSharp.text.PageSize.A4.Rotate());
        PdfPTable t = new PdfPTable(cols);//arialuni
        BaseFont bf = BaseFont.CreateFont("c:/windows/fonts/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        Font f1 = new iTextSharp.text.Font(bf, 15, Font.NORMAL);
        Font f2 = new iTextSharp.text.Font(bf, 5, Font.NORMAL);
        t.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
        var columnwidth = new[] { 1f, 3f, 3f, 1.5f, 1f, 1f, 1f, 1.5f, 2f, 2f, 1.6f, 4f, 4f, 4f, 3f, 3f, 5f, 3f };
        //cell object
        PdfPCell c, c1;
        //create new font based on BaseFont
        Font fontContent = new Font(bf, 7);
        Font fontHeader = new Font(bf, 9);
        PdfWriter.GetInstance(doc, Page.Response.OutputStream);
        t.WidthPercentage = 100;
        t.SetWidths(columnwidth);
        doc.Open();
        Chunk title = new Chunk("候補者確定一覧 \n\n", f1);
        Paragraph p = new Paragraph();
        PdfPTable t2 = new PdfPTable(cols);
        for (int j1 = cols - 1; j1 >= 0; j1--)
        {
            t2.SetWidths(columnwidth);
            t2.WidthPercentage = 100;
            Phrase pr = new Phrase((dt.Columns[j1].Caption != null &&
              dt.Columns[j1].Caption.Length > 0) ? dt.Columns[j1].Caption :
              dt.Columns[j1].ColumnName, fontHeader);
            c1 = new PdfPCell(pr);
            c1.BackgroundColor = new BaseColor(169, 169, 169);
            c1.HorizontalAlignment = Element.ALIGN_CENTER;
            t2.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
            t2.AddCell(c1);
        }
        //write table content
        for (int i = 1; i < rows + 1; i++)
        {
            for (int j = cols - 1; j >= 0; j--)
            {
                string temp = dt.Rows[i - 1][j].ToString();
                temp = temp.Replace("(", " ");
                temp = temp.Replace(")"," ");
                temp = temp.Replace(".", "");
                c = new PdfPCell(new Phrase(temp, fontContent));
                if (i % 2 != 0)
                {
                    c.BackgroundColor = new BaseColor(255, 228, 196);//169-169-169
                }
                else
                {
                    c.BackgroundColor = BaseColor.WHITE;
                }
                c.HorizontalAlignment = Element.ALIGN_RIGHT;
                t.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
                t.AddCell(c);
            }
            if (i % 20 == 0)
            {
                PdfPTable t1 = new PdfPTable(cols);
                for (int j1 = cols - 1; j1 >= 0; j1--)
                {
                    t1.SetWidths(columnwidth);
                    t1.WidthPercentage = 100;
                    Phrase pr = new Phrase((dt.Columns[j1].Caption != null &&
                      dt.Columns[j1].Caption.Length > 0) ? dt.Columns[j1].Caption :
                      dt.Columns[j1].ColumnName, fontHeader);
                    c1 = new PdfPCell(pr);
                    c1.BackgroundColor = new BaseColor(169, 169, 169);
                    c1.HorizontalAlignment = Element.ALIGN_CENTER;
                    t1.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
                    t1.AddCell(c1);
                }
                doc.NewPage();
                p.Alignment = Element.ALIGN_CENTER;
                p.Add(title);
                doc.Add(p);
                doc.Add(t1);
                doc.Add(t);
                p = new Paragraph();
                t = new PdfPTable(cols);
                t.SetWidths(columnwidth);
                t.WidthPercentage = 100;
            }

        }
        doc.NewPage();
        p.Alignment = Element.ALIGN_CENTER;
        p.Add(title);
        doc.Add(p);
        doc.Add(t2);
        doc.Add(t);
        doc.Close();
        Page.Response.ContentType = "application/pdf";
        string filename = "TraineeList_" + string.Format("{0:yyyyMMdd}", DateTime.Now) + ".pdf";
        Page.Response.AddHeader("content-disposition", "attachment;filename=" + filename + "");
        Page.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Page.Response.Write(doc);
        Page.Response.End();
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Get error message from id
    /// </summary>
    /// <param name="errorid"></param>
    /// <returns></returns>
    public String GetError(string errorid)
    {
        string error = "";
        Error er = new Error();
        er.ErrorID = errorid;
        ErrorManager frm_error = new ErrorManager();
        DataTable dt = frm_error.getError(er);
        if (dt.Rows.Count > 0)
        {
            error = dt.Rows[0][2].ToString();
        }
        return error;
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Event: Click ddl_組合選択 load ddl_組合員企業選択 where 企業ID
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ddl_組合選択_SelectedIndexChanged(object sender, EventArgs e)
    {
        Load_ddl_組合員企業選択(Convert.ToInt32(ddl_組合選択.SelectedValue));
        //if (hd_BrowserVer.Value == "true")
        //{
        //    ddl_組合員企業選択.Focus();
        //}
        //else
        //{
        //    ddl_組合選択.Focus();
        //}
        ScriptManager.RegisterStartupScript(up, GetType(), "Javascript", "javascript: RegisterPage(); ", true);
        ddl_組合員企業選択.Focus();
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Check exists ID in table 面接情報
    /// </summary>
    /// <param name="面接日"></param>
    /// <param name="団体ID"></param>
    /// <param name="企業ID"></param>
    /// <returns></returns>
    public DataTable frmJ_TraineeSearch_CheckExists(DateTime 面接日, int 団体ID, int 企業ID)
    {
        DataTable dt = _応募情報Manager.frmJ_TraineeSearch_CheckExists応募情報(面接日, 団体ID, 企業ID);
        return dt;
    }


    /// <summary>
    /// Coder: Thanh Dong
    /// Event: Ok of Confirm message
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn_Ok_Click(object sender, EventArgs e)
    {
        Session["frmJ_TraineeSearch_Mode"] = "update";
        Session["frmJ_TraineeSearch_Search"] = frmJ_TraineeSearch_Search();
        Session["frmJ_TraineeSearch_ddl_組合選択"] = Convert.ToInt32(ddl_組合選択.SelectedValue);
        Session["frmJ_TraineeSearch_ddl_組合選択_text"] = ddl_組合選択.SelectedItem.Text;
        Session["frmJ_TraineeSearch_ddl_組合員企業選択"] = Convert.ToInt32(ddl_組合員企業選択.SelectedValue);
        Session["frmJ_TraineeSearch_ddl_組合員企業選択_Text"] = ddl_組合員企業選択.SelectedItem.Text;
        Session["frmJ_TraineeSearch_txt_面接日"] = txt_面接日.Text;
        Response.Redirect("frmJ_TraineeListSelect.aspx");
    }


    protected void btn_リセット_Click(object sender, EventArgs e)
    {
        lbl_Count.Text = "";
        Session.Remove("frmJ_TraineeSearch_flag");
        ddl_年齢1.Focus();
        ddl_年齢1.SelectedValue = "-1";
        ddl_年齢2.SelectedValue = "-1";
        ddl_性別.SelectedValue = "-1";
        ddl_最終学歴1.SelectedValue = "-1";
        ddl_最終学歴2.SelectedValue = "-1";
        ddl_就業経験有無.SelectedValue = "-1";
        txt_身長.Text = "";
        txt_体重1.Text = "";
        txt_体重2.Text = "";
        ddl_視力.SelectedValue = "-1";
        ddl_日本語学習経験.SelectedValue = "-1";
        ddl_経験作業名.SelectedValue = "-1";

    }
    protected void ddl_就業経験有無_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddl_就業経験有無.SelectedValue.ToString() == "1")
        {
            Load_ddl_経験作業名();
        }
        else
        {
            DataTable dt = new DataTable();
            ddl_経験作業名.DataSource = dt;
            ddl_経験作業名.DataTextField = "事業内容";
            ddl_経験作業名.DataValueField = "ID";
            ddl_経験作業名.DataBind();
            System.Web.UI.WebControls.ListItem item = new System.Web.UI.WebControls.ListItem("[選択]", "-1");
            item.Selected = true;
            ddl_経験作業名.Items.Insert(0, item);
        }
    }
}
